/**
 * copyright lexio 2006
 * Copyright geolabs 2006-2007
 * author: Gérald Fenoy
 *
 */

GridControler=Controler.extend();
GridControler.define({

  _init: function(){
      this.name=arguments[0].name;
      this.limit=arguments[0].limit;
      this.offset=arguments[0].offset;
      this.nbScrolling=arguments[0].scroll;
      this.ref=(arguments[0].ref ? arguments[0].ref : null);

      this.loading=(arguments[0].loading ? System.document.getElementById(arguments[0].loading) : null);

      this.values=new Array();
      this.currPage=0;
      
      this.columnsName=
	new Array('grid_id','grid_label');

      this.columnsSetup=
	new Array('setupDefault','setupDefault');
      
      this.varName=
	new Array('id','value');
      
        this.request=null;
    },

  setCurrPage: function(){
      this.currPage=arguments[0];
      this.updateScrollingMachine();
      try{this.removeOld();}catch(e){alert("octodebug removeold"+e);}
      try{this.updateContent();}catch(e){alert("octodebug updateSearch +"+e);}
    },

  next: function(){
      if(this.currPage+1<this.nbPages)
	this.setCurrPage(this.currPage+1);
    },

  previous: function(){
      if(this.currPage-1>=0)
	this.setCurrPage(this.currPage-1);
    },

  removeOld: function(){
      for(i=0;i<this.limit;i++){
	for(j=0;j<this.columnsName.length;j++){
	  currElement=System.document.getElementById(this.columnsName[j]+"_"+(i+1));
	  if(currElement)
	    switch(currElement.tagName){
	    case "IMG":
	      currElement.style.display='none';
	      break;
	    case "INPUT":
	      currElement.value='-1';
	      break;
	    default:
	      currElement.innerHTML="&nbsp;";
	      break;
	    }
	}
      }
    },

  isEmpty: function(){
      if(!this.tuples[0].hasAttribute){
	if(this.tuples[0].getAttribute('empty') != null)
	  return true;
      }
      else{
	if(this.tuples[0].hasAttribute('empty')) 
	  return true;
      }
      return false;
    },

  updateValues: function(){
      this.currPage=0;
      
      this.values=new Array();
      for(i=0;i<this.tuples.length;i++){
	this.values[i]=new Array();
	for(j=0;j<this.varName.length;j++){
	  varName=this.varName[j];
	  allVarName=this.tuples[i].getElementsByTagName(varName);
	  if(allVarName.length>0){
	    currElement=allVarName[0];
	    //alert("ie "+currElement);
	    if(currElement.firstChild)
	      this.values[i][varName]=currElement.firstChild.nodeValue;
	    else this.values[i][varName]="";
	  }
	  else{
	    this.values[i][varName]="";
	  }
	}
      }      
      /**
       * get the number of pages (@see README.js.txt)
       */
      if(!this.isEmpty() && this.tuples.length > 1){
	tmp = this.values.length%this.limit;
	if(tmp>0){
	  this.nbPages = ((this.values.length-tmp)/this.limit)+1;
	}
	else this.nbPages=this.values.length/this.limit;
      }
      else{
	this.nbPages=0;
      }      
    },

  updateContent: function(){
      nbElements=this.values.length;
      
      this.displayStatus();
      
      if(!this.isEmpty() && nbElements>0){
	/*System.document.getElementById('nb_tuples').innerHTML=nbElements;
	  System.document.getElementById('nb_comment').innerHTML=(nbElements>1) ? "&nbsp;organismes de formations correspondent à votre recherche" : "&nbsp;organisme de formation correspond à votre recherche";*/
      }
      else{
	nbElements=0;
	/*System.document.getElementById('nb_tuples').innerHTML=0;
	  System.document.getElementById('nb_comment').innerHTML="&nbsp;(Aucun organisme de formation ne correspond à vos critères de recherche";*/
      }
      
      offset=this.currPage*this.limit;
      
      limit=(this.limit>nbElements) ? nbElements : this.limit;
      
      for(i=0;i<limit;i++){
	if((i+offset)<nbElements){
	  for(j=0;j<this.columnsName.length;j++){
	    currElement=System.document.getElementById(this.columnsName[j]+"_"+(i+1));
	    if(currElement)
	      switch(currElement.tagName){
	      case "IMG":
		currElement.style.display='none';
		currElement.setAttribute('alt',"");
		currElement.setAttribute('title',"");
		eval("this."+this.columnsSetup[j]+"("+(i+offset)+",currElement)");
		break;
	      case "INPUT":
		currElement.value=eval("this."+this.columnsSetup[j]+"("+(i+offset)+","+j+")");
		break;
	      default:
		currElement.innerHTML=eval("this."+this.columnsSetup[j]+"("+(i+offset)+","+j+")");
		break;
	      }
	  }
	}
      }
    },

  updateScrollingMachine: function(){
      for(var index=0;index<this.nbScrolling;index++){
	var myMachine=System.document.getElementById('scrollingMachine'+index);
	myMachine.innerHTML=this.nbPages+" ";
	tmp="<table><tr>";
	if(!this.isEmpty() && this.nbPages>1){
	  for(i=0;i<this.nbPages;i++){
	    var append=(this.ref?this.ref+'.':'');
	    if(i==0)
	      tmp+="<td id=\"paginator_previous_"+index+"_"+i+"\"> <a class=\"paginator\" href=\"javascript: "+append+this.name+".previous();\" title=\"visualiser la page "+(i+1)+"\" alt=\"void\" > précédent </a> </td>";
	    tmp+="<td id=\"paginator_"+index+"_"+i+"\"> <a class=\"paginator\" href=\"javascript: "+append+this.name+".setCurrPage("+i+");\" title=\"visualiser la page "+(i+1)+"\" alt=\"void\" >"+(i+1)+"</a> </td>";
	    if(i+1==this.nbPages)
	      tmp+="<td id=\"paginator_next_"+index+"_"+i+"\"> <a class=\"paginator\" href=\"javascript: "+append+this.name+".next();\" title=\"visualiser la page "+(i+1)+"\" alt=\"void\" > suivant </a> </td>";
	    
	  }
	}
	tmp+="</tr></table>";
	myMachine.innerHTML=tmp;
	
	if(this.nbPages>1){
	  System.document.getElementById("paginator_"+index+"_"+this.currPage).style.backgroundColor="#89B7BE";
	  System.document.getElementById("paginator_"+index+"_"+this.currPage).getElementsByTagName('a')[0].style.color="#fff";
	  
	  for(i=0;i<this.nbPages;i++)
	    if(i!=this.currPage){
	      System.document.getElementById("paginator_"+index+"_"+i).style.backgroundColor="#fff";
	      System.document.getElementById("paginator_"+index+"_"+i).getElementsByTagName('a')[0].style.color="#990033";
	    }
	}
	
      }
      
    },

  setupDefault: function(){
      //return this.values[tuple_index][this.varName[value_index]];
      return this.values[arguments[0]][this.varName[arguments[1]]];
    },

  displayStatus: function(){
      //alert(this.loading);
      if(this.loading)
	if(this.request){
	  this.loading.style.display='block';
	}
	else
	  this.loading.style.display='none';
      /*var tmp=System.document.createElement('span');
	tmp.id='tmp';
	tmp.appendChild(System.document.createTextNode(' '));
	this.loading.parentNode.appendChild(tmp);
	}*/
    },

  newData: function(){
      //this.isempty();
      var xmldoc = this.request.responseXML;
      this.request=null;
      this.displayStatus();
      //var rnode = xmldoc.getElementsByTagName('tuple');
      this.tuples=xmldoc.getElementsByTagName('tuple');
      /**
       * Update the search result content
       */
      this.updateValues();
      this.updateScrollingMachine()
      this.setCurrPage(0);
      //System.document.getElementById('searchStatus').innerHTML="&nbsp;";
    }

  });
